﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using Arch;
using InvoiceChecking.Data;
using InvoiceChecking.Data.SqlClient;
using Microsoft.Reporting.WebForms;

public partial class Admin_ReportCreditCardPayment : System.Web.UI.Page
{
    private DataSet invoiceDetails;
    protected void Page_Load(object sender, EventArgs e)
    {
        ReportViewer1.LocalReport.SubreportProcessing += LocalReport_SubreportProcessing;
        if (Request.QueryString["BatchNo"] != null)
        {
            int batchNo = Convert.ToInt32(Request.QueryString["BatchNo"]);
            int allocRef = Convert.ToInt32(Request.QueryString["AllocRef"]);
            int creditCardId = Convert.ToInt32(Request.QueryString["CreditCardId"]);

            invoiceDetails = DataRepository.SunInvoiceProvider.InvoiceDetails(Convert.ToInt32(allocRef));
            SqlCommand sqlCommand = new SqlCommand
            {
                CommandText = "_SunInvoice_GetCreditCard",
                CommandType = CommandType.StoredProcedure
            };
            sqlCommand.Parameters.AddWithValue("JrnalNo", batchNo);
            sqlCommand.Parameters.AddWithValue("AllocRef", allocRef);
            sqlCommand.Parameters.AddWithValue("CreditCardId", creditCardId);
            DataSet invoices = DataRepository.Provider.ExecuteDataSet(sqlCommand);

            ReportViewer1.LocalReport.DataSources["DSExportPayment_CreditCard"].Value = invoices.Tables[0];
        }
        else
        {
            if (Session["VoucherInvoice"] != null)
            {
                var invoices = (DataSet)Session["VoucherInvoice"];
                invoiceDetails = (DataSet)Session["VoucherInvoiceDetails"];
                ReportViewer1.LocalReport.DataSources["DSExportPayment_CreditCard"].Value = invoices.Tables[0];
            }
        }
        //ReportViewer1.LocalReport.DataSources["DSExportPayment_InvoiceDetails"].Value = .Tables[0];
        ReportViewer1.DataBind();
    }

    private void LocalReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e)
    {
        e.DataSources.Add(new ReportDataSource("DSExportPayment_InvoiceDetails", invoiceDetails.Tables[0]));
    }

    //protected override void OnInit(EventArgs e)
    //{
    //    base.OnInit(e);
    //    HttpCookie httpCookie = Request.Cookies["ICConnectionString"];
    //    if (httpCookie != null)
    //    {
    //        SqlNetTiersProvider provider = GlobalUtilities.CreateSunProvider(Server.MapPath("~") + "/Config.xml",
    //                                                                      httpCookie.Value);
    //        if (provider != null)
    //            DataRepository.LoadProvider(provider, true);
    //    }
    //}
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        HttpCookie httpCookie = Request.Cookies["ICConnectionString"];
        if (httpCookie != null)
        {
            SqlNetTiersProvider provider = GlobalUtilities.CreateSunProvider(Server.MapPath("~") + "/Config.xml",
                                                                          httpCookie.Value);
            if (provider != null)
                DataRepository.LoadProvider(provider, true);
        }
    }
}
